package org.csu.sei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.sei.entity.News;
import org.csu.sei.entity.NewsContent;
import org.csu.sei.persistence.NewsContentMapper;
import org.csu.sei.service.INewsContentService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

@Component(value = "newsContentService")
public class NewsContentimpl extends ServiceImpl<NewsContentMapper, NewsContent> implements INewsContentService {

    @Resource
    private NewsContentMapper newsContentMapper;

    @Override
    public NewsContent queryNewsContent(Long newsId) {
        QueryWrapper<NewsContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_id",newsId);
        return newsContentMapper.selectOne(queryWrapper);
    }

    @Override
    public int insertNewsContent(NewsContent newsContent) {
        newsContent.setCreateTime(LocalDateTime.now());
        newsContent.setUpdateTime(LocalDateTime.now());
        return newsContentMapper.insert(newsContent);
    }

    @Override
    public int updateNewsContent(NewsContent newsContent) {
        newsContent.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<NewsContent> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(NewsContent::getContent,newsContent.getContent()).
                eq(NewsContent::getNewsId,newsContent.getNewsId());
        return newsContentMapper.update(null,updateWrapper);
    }

    @Override
    public int deleteNewsContent(Long newsId) {
        return newsContentMapper.deleteById(newsId);
    }

    @Override
    public int deleteByNewsId(Long newsId) {
        QueryWrapper<NewsContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("news_id",newsId);
        return newsContentMapper.delete(queryWrapper);
    }


}
